import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { homeServiceModule } from "@/services";
export const getAsyncGoodPriceAction = createAsyncThunk(
  "fetch/goodPriceInfo",
  (payload, { dispatch }) => {
    homeServiceModule.fetchGoodPrice().then((data) => {
      dispatch(changeGoodPriceListAction(data));
    });
    homeServiceModule.fetchHighScoreRooms().then((data) => {
      dispatch(changeHighScoreListAction(data));
    });
    homeServiceModule.fetchDisCountRooms().then((data) => {
      dispatch(changeDiscountListAction(data));
    });
    homeServiceModule.fetchHomeRecommand().then((data) => {
      dispatch(changeHomeRecommandListAction(data));
    });
    homeServiceModule.fetchLongForHomeList().then((data) => {
      dispatch(changeHomeLongForListAction(data));
    });
    homeServiceModule.fetchHomePlusList().then((data) => {
      dispatch(changeHomePlusListAction(data));
    });
  }
);
const home = createSlice({
  name: "home",
  initialState: {
    goodPriceInfo: {},
    highScoreInfo: {},
    discountInfo: {},
    homeRecommandInfo: {},
    longForInfo: {},
    homePlusInfo: {},
  },
  reducers: {
    changeGoodPriceListAction(state, { payload }) {
      state.goodPriceInfo = payload;
    },
    changeHighScoreListAction(state, { payload }) {
      state.highScoreInfo = payload;
    },
    changeDiscountListAction(state, { payload }) {
      state.discountInfo = payload;
    },
    changeHomeRecommandListAction(state, { payload }) {
      state.homeRecommandInfo = payload;
    },
    changeHomeLongForListAction(state, { payload }) {
      state.longForInfo = payload;
    },
    changeHomePlusListAction(state, { payload }) {
      state.homePlusInfo = payload;
    },
  },
  // extraReducers: {
  //   [getAsyncGoodPriceAction.fulfilled](state, { payload }) {
  //     console.log("payload1", payload);
  //     state.goodPriceInfo = payload;
  //   },
  // },
});
export const {
  changeGoodPriceListAction,
  changeHighScoreListAction,
  changeDiscountListAction,
  changeHomeRecommandListAction,
  changeHomeLongForListAction,
  changeHomePlusListAction,
} = home.actions;

export default home.reducer;
